package cn.com.api.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.api.entity.ApiT_shops_time;
import cn.com.api.mapper.ApiT_shops_timeMapper;
import cn.com.api.mapper.ApiT_tokenMapper;
import cn.com.api.service.ApiShopsTimeService;
/*import cn.com.web.mapper.ShopsMapper;*/
import cn.com.util.*;
import cn.com.util.*;

@Service
public class ApiShopsTimeServiceImpl implements ApiShopsTimeService {
	@Autowired
	private ApiT_shops_timeMapper apiShopsTimeMapper;
/*	@Autowired
	private ShopsMapper shopsMapper;*/
	@Autowired
	private ApiT_tokenMapper apiT_tokenMapper;

	@Override
	public boolean insert(ApiT_shops_time T_shops_time) {
		return apiShopsTimeMapper.insert(T_shops_time);
	}

	@Override
	public List<ApiT_shops_time> findT_shops_timeByShopId(String shopid) {
		return apiShopsTimeMapper.findT_shops_timeByShopId(shopid);
	}

	@Override
	public boolean update(ApiT_shops_time T_shops_time) {
		return apiShopsTimeMapper.update(T_shops_time);
	}
	
	@Transactional
	@Override
	public void updateShopTime(ApiT_shops_time shopTime,Map<String, Object> map) throws Exception{
		Integer shop_id = shopTime.getShop_id();
		List<ApiT_shops_time> list = findT_shops_timeByShopId(shop_id+"");
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = dateFormat.format(new Date());
		shopTime.setUpdate_date(date);
		
		if(list!=null && list.size()>0){//正常情况下只能从数据库查询到一条
			apiShopsTimeMapper.updateT_shops_time(shopTime);
		}else{
			shopTime.setCreate_date(date);
			apiShopsTimeMapper.insertT_shops_time(shopTime);
		}
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "修改成功");
	}
	
	@Override
	public boolean checkNowDateInShopTime(Integer shop_id) {
		boolean flag = false;//true -在营业时间  false-不在营业时间
		//当天是否营业  周几  ，是否在当天的营业时间
		List<ApiT_shops_time> list = apiShopsTimeMapper.findT_shops_timeByShopId(shop_id+"");
		if(list!=null && list.size()>0){
			ApiT_shops_time shopTime = list.get(0);
			Date date = new Date();
			long nowDate = date.getTime();
			String weekDay = date.getDay()+"";//0-7
			if("0".equals(weekDay)){
				weekDay = "7";
			}
			String business = shopTime.getBusiness_type();
			if(!Util.isNull(business) && !business.contains(weekDay)){//
				flag = false;//当天未营业  打烊
			}else{
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
				String firstDate = dateFormat.format(date);
				String[] start_time = shopTime.getStart_time().split(",");
				String[] end_time = shopTime.getEnd_time().split(",");
				if(start_time!=null && start_time.length>0){
					try {
						int length = start_time.length;
						for(int i=0;i<length;i++){
							//开始结束时间
							Date startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
								.parse(firstDate + " " + start_time[i]+":00");
							//结束时间
							Date endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
								.parse(firstDate + " " + end_time[i]+":00");
							long startTime = startDate.getTime();
							long endTime = endDate.getTime();
							if(startTime<nowDate && nowDate<endTime){
								flag = true;
								break;
							}
						}
					} catch (ParseException e) {
						e.printStackTrace();
					}
				}
			}
		}else{
			flag = true;
		}
		return flag;
	}

}
